package com.uni.crawl.service.impl.crawl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.uni.crawl.common.enums.crawl.CrawlQueueStatusEnum;
import com.uni.crawl.mapper.CrawlQueueMapper;
import com.uni.crawl.model.entity.crawl.CrawlNamespace;
import com.uni.crawl.model.entity.crawl.CrawlQueue;
import com.uni.crawl.model.entity.crawl.CrawlTemplate;
import com.uni.crawl.model.query.crawl.CrawlQueuePageQuery;
import com.uni.crawl.model.vo.crawl.CrawlQueueVO;
import com.uni.crawl.service.PageQueryUtils;
import com.uni.crawl.service.crawl.CrawlQueueService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 爬虫队列 服务实现类
 * </p>
 *
 * @author JervisPG
 * @since 2023-12-27
 */
@Service
public class CrawlQueueServiceImpl extends MPJBaseServiceImpl<CrawlQueueMapper, CrawlQueue> implements CrawlQueueService {

    @Override
    public void setQueueStatus(Long id, CrawlQueueStatusEnum statusEnum) {
        LambdaUpdateWrapper<CrawlQueue> wp = Wrappers.lambdaUpdate();
        wp.eq(CrawlQueue::getId, id);

        wp.set(CrawlQueue::getCrawlStatus, statusEnum.getStatus());
        wp.set(CrawlQueue::getUpdateTime, LocalDateTime.now());

        update(wp);
    }

    @Override
    public boolean isExists(Long instanceId, String urlMd5) {
        LambdaUpdateWrapper<CrawlQueue> wp = Wrappers.lambdaUpdate();
        wp.eq(CrawlQueue::getInstanceId, instanceId);
        wp.eq(CrawlQueue::getUrlMd5, urlMd5);
        return count(wp) > 0;
    }

    @Override
    public Page<CrawlQueueVO> getPage(CrawlQueuePageQuery pageQuery) {
        String nsCode = pageQuery.getNsCode();
        String tmplCode = pageQuery.getTmplCode();

        MPJLambdaWrapper<CrawlQueue> wp = MPJWrappers.lambdaJoin();
        wp.selectAll(CrawlQueue.class);
        wp.select(CrawlNamespace::getNsName);
        wp.select(CrawlTemplate::getTmplName);

        wp.leftJoin(CrawlNamespace.class, CrawlNamespace::getNsCode, CrawlQueue::getNsCode);
        wp.leftJoin(CrawlTemplate.class, CrawlTemplate::getTmplCode, CrawlQueue::getTmplCode);

        wp.eq(Objects.nonNull(pageQuery.getId()), CrawlQueue::getId, pageQuery.getId());
        wp.eq(StrUtil.isNotEmpty(nsCode), CrawlQueue::getNsCode, nsCode);
        wp.like(StrUtil.isNotEmpty(tmplCode), CrawlQueue::getTmplCode, tmplCode);
        wp.eq(Objects.nonNull(pageQuery.getInstanceId()), CrawlQueue::getInstanceId, pageQuery.getInstanceId());
        wp.eq(Objects.nonNull(pageQuery.getCrawlStatus()), CrawlQueue::getCrawlStatus, pageQuery.getCrawlStatus());
        PageQueryUtils.wrapTimeRange(wp, CrawlQueue::getCreateTime, pageQuery);

        wp.orderByDesc(CrawlQueue::getId);

        return selectJoinListPage(PageQueryUtils.getPage(pageQuery), CrawlQueueVO.class, wp);
    }
}
